package com.lc.luzhengtang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.LoginActivity;
import com.lc.luzhengtang.activity.NewsListActivity;
import com.lc.luzhengtang.activity.SearchHistoryActivity;
import com.lc.luzhengtang.bean.IndexBean;
import com.lc.luzhengtang.bean.IndexType;
import com.lc.luzhengtang.conn.ConnGetType;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/2.
 */

public class IndexFragment extends BaseFragment implements OnTabSelectListener {
    /*    @BindView(R.id.indicator)
        TabPageIndicator indicator;*/
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    Unbinder unbinder;
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    /**
     * 自定义部分属性
     */


    SlidingTabLayout tl2;
    private Context mContext = getActivity();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private List<String> list_title = new ArrayList<>();

    private final String[] mTitles = {
            "胶囊", "清热解毒", "药片"

    };

    //
//    public static List<IndexBean.DataBean.GoodstypeBean> goodstypeBeen;
    List<IndexBean.DataBean.InfoBean> infoBeen;
    private MyPagerAdapter mAdapter;

    List<IndexType.DataBean> dataBeen;

    List<String> getList_title = new ArrayList<>();
    List<String> getlist_ids = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_index, null);
        unbinder = ButterKnife.bind(this, view);
        //        goodstypeBeen = new ArrayList<>();

        // getBannerData();
        dataBeen = new ArrayList<>();
/*
        for (int i = 0; i < goodstypeBeen.size(); i++) {
            mFragments.add(SimpleCardFragment.getInstance(goodstypeBeen.get(i).getId()+""));
        }*/

        tl2 = (SlidingTabLayout) view.findViewById(R.id.tl_2);
        tl2.setOnTabSelectListener(this);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getIndexTypeData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.img_news, R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                if (TextUtils.isEmpty(BaseApplication.basePreferences.readUid())) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), NewsListActivity.class));
                }

                break;
            case R.id.iv_search:
                startActivity(new Intent(getActivity(), SearchHistoryActivity.class));

                break;
        }
    }


    public void getIndexTypeData() {
        new ConnGetType(new AsyCallBack<IndexType>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, IndexType indexType) throws Exception {
                super.onSuccess(toast, type, o, indexType);
                dataBeen = indexType.getData();
                if (indexType != null) {
                    getList_title.clear();
                    for (int i = 0; i < dataBeen.size(); i++) {
                        getList_title.add(dataBeen.get(i).getTitle());
                    }
                    getList_title.add(0, "全部分类");
                    getlist_ids.clear();
                    for (int i = 0; i < dataBeen.size(); i++) {
                        getlist_ids.add(dataBeen.get(i).getId() + "");
                    }
                    getlist_ids.add(0, "0");

                    // infoBeen.addAll(indexBean.getData().getInfo());
                    for (int i = 0; i < getlist_ids.size(); i++) {
//                    mFragments.add(SimpleCardFragment.getInstance(dataBeen.get(i).getId() + ""));
                        mFragments.add(SimpleCardFragment.getInstance(getlist_ids.get(i)));
                    }
                    mAdapter = new MyPagerAdapter(getFragmentManager());
                    viewPager.setAdapter(mAdapter);
                    tl2.setViewPager(viewPager);
                }

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }


            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }
        }
      ).execute(getActivity(), false);
    }

    @OnClick(R.id.tl_2)
    public void onViewClicked() {
    }

    @Override
    public void onTabSelect(int position) {
//        Toast.makeText(getActivity(), "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabReselect(int position) {
//        Toast.makeText(getActivity(), "onTabReselect&position--->" + position, Toast.LENGTH_SHORT).show();
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return getList_title.size();
            //   return mTitles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
//
//            return mTitles[position];
            return getList_title.get(position);

        }

        @Override
        public Fragment getItem(int position) {
            //  return mFragments.get(position);
            return mFragments.get(position);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        getIndexTypeData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
